package com.kimho.inhouse.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kimho.core.PageInfo;
import com.kimho.inhouse.domain.Inhouse;
import com.kimho.inhouse.service.InhouseService;
import com.kimho.user.domain.User;
import com.kimho.util.Util;

@Controller
@RequestMapping("/inhouse")

public class InhouseController {

	@Autowired
	private InhouseService inhouseService;
	/**
	 * 登录验证
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/items",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> getInhouseList(HttpServletRequest request,   
            HttpServletResponse response){
		if(request.getParameter("stop")!=null && "fangzhishuaxin".equals(request.getParameter("stop"))){//防止用户没有选择物流单刷新下面的 grid
			return null;
		}
		Map<String,Object> responseMap = new HashMap<String,Object>();
		PageInfo pi = new PageInfo(request);
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("start", pi.getStart());
		param.put("limit", pi.getLimit());
		param.put("query", request.getParameter("query"));
		param.put("logistics_id", request.getParameter("logistics_id"));
		param.put("notin", request.getParameter("notin"));//取出没有加入物流单号的标示
		if((User)request.getSession().getAttribute("user")!=null && ((User)request.getSession().getAttribute("user")).getPost_id()!=1){//如果登陆用户的较色不是1(经理),就只查询登陆地点的数据
			param.put("login_location", request.getSession().getAttribute("login_localtion"));
		}
		List<Inhouse> inhouse = inhouseService.getInhouseList(param);
		responseMap.put("totalCount", inhouseService.getTotal(param));
		responseMap.put("rows", inhouse);
		return responseMap;
	}
	
	@RequestMapping(value="/items_o",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> getInhouseList_o(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		PageInfo pi = new PageInfo(request);
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("start", pi.getStart());
		param.put("limit", pi.getLimit());
		param.put("query", request.getParameter("query"));
		param.put("logistics_id", request.getParameter("logistics_id"));
		param.put("notin", request.getParameter("notin"));//取出没有加入物流单号的标示
		List<Inhouse> inhouse = inhouseService.getInhouseList_o(param);
		responseMap.put("totalCount", inhouseService.getTotal_o(param));
		responseMap.put("rows", inhouse);
		return responseMap;
	}
	
	@RequestMapping(value="/items_t",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> getInhouseList_t(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		PageInfo pi = new PageInfo(request);
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("start", pi.getStart());
		param.put("limit", pi.getLimit());
		param.put("logistics_id", request.getParameter("logistics_id"));
		param.put("notin", request.getParameter("notin"));//取出没有加入物流单号的标示
		List<Inhouse> inhouse = inhouseService.getInhouseList_t(param);
		responseMap.put("totalCount", inhouseService.getTotal_t(param));
		responseMap.put("rows", inhouse);
		return responseMap;
	}
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> saveOrUpdate(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		String object_id = request.getParameter("object_id");
		String from_addr = request.getParameter("from_addr");
		String from_name = request.getParameter("from_name");
		String from_tell = request.getParameter("from_tell");
		String to_addr = request.getParameter("to_addr");
		String to_name = request.getParameter("to_name");
		String to_tell = request.getParameter("to_tell");
		String brief = request.getParameter("brief");
		String ppice = request.getParameter("ppice");
		String price = request.getParameter("price");
		String traff_fee=request.getParameter("traff_fee");
		String mid_fee	=request.getParameter("mid_fee");
		String oth_fee	=request.getParameter("oth_fee");
		String inst_fee =request.getParameter("inst_fee");
		String staff_id = ((User)request.getSession().getAttribute("user")).getStaff_id();
		String objid = request.getParameter("objid");
		String obj_name=request.getParameter("obj_name");
		String obj_num=request.getParameter("obj_num");
		String transitf = request.getParameter("transitf");
		String transit_id=request.getParameter("transit_id");
		String pre_fee = request.getParameter("pre_fee");
		String cost_type=request.getParameter("cost_type");
		String send_type=request.getParameter("send_type");
		String to_cmp=request.getParameter("to_cmp");
		try {
			if(request.getSession().getAttribute("login_localtion")==null && request.getSession().getAttribute("login_localtion").equals("")){
				responseMap.put("success", false);
				responseMap.put("info", "新增失败,获取登陆地点失败,请重新登陆！");
				return responseMap;
			}
			String in_cmp=(String) request.getSession().getAttribute("login_localtion");//取得登陆地点,作为入库地点
			Inhouse inhouse = new Inhouse();
		    inhouse.setIn_cmp(in_cmp);//设置入库物流部
		    inhouse.setTo_cmp(to_cmp);//设置收货物流部
		    inhouse.setFrom_addr(from_addr);
		    inhouse.setFrom_name(from_name);
		    inhouse.setFrom_tell(from_tell);
		    inhouse.setTo_addr(to_addr);
		    inhouse.setTo_name(to_name);
		    inhouse.setTo_tell(to_tell);
		    inhouse.setPpice(ppice);
		    inhouse.setSend_type(send_type);
		    
		    if(ppice.equals("1")){
		    	inhouse.setPrice(Double.parseDouble(price));
		    }else{
		    	inhouse.setPrice(0.0d);
		    }
		    inhouse.setBrief(brief);
		    inhouse.setTraff_fee(Double.parseDouble(traff_fee));
		    inhouse.setMid_fee(Double.parseDouble(mid_fee));
		    inhouse.setOth_fee(Double.parseDouble(oth_fee));
		    inhouse.setInst_fee(Double.parseDouble(inst_fee));
		    inhouse.setOver("1");//设置物件状态为1,表示货物入库
		    inhouse.setCost_type(cost_type);
		    inhouse.setStaff_id(staff_id);
		    inhouse.setObjid(objid);
		    inhouse.setObj_name(obj_name);
		    inhouse.setObj_num(obj_num);
		    inhouse.setPre_fee(Double.parseDouble(pre_fee));
		    inhouse.setTransitf(transitf);//1为中转,保存中转公司ID(transit_id),否则为空
		    inhouse.setTransit_id(transit_id);
			//编辑货单信息
			if(object_id!=null && !"".equals(object_id)) {
				inhouse.setObject_id(object_id);
				inhouseService.updateInhouse(inhouse);
				responseMap.put("success", "true");
				responseMap.put("info", "编辑成功！");
			}
			//新增货单信息
			else {
				Map<String,Object> param = new HashMap<String,Object>();
			    inhouse.setObject_id(Util.getUUID());
				inhouseService.insertInhouse(inhouse, param);
				responseMap.put("method", "Create");
				responseMap.put("success", "true");
				responseMap.put("info", "新增成功！");
			}
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("info", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}
	/**
	 * 删除用户
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/remove",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> remove(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		String ids = request.getParameter("ids");
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("ids", ids);
		try {
			inhouseService.deleteInhouse(param);
			responseMap.put("success", "true");
			responseMap.put("info", "删除成功！");
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("info", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}


}
